package com.example.e_comm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.e_comm.entity.Permission;
import com.example.e_comm.entity.User;
import com.example.e_comm.mapper.PermissionMapper;
import com.example.e_comm.mapper.UserMapper;
import com.example.e_comm.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Transactional
public class PermissionServiceImpl
        extends ServiceImpl<PermissionMapper, Permission>
        implements PermissionService {
    @Autowired
    private PermissionMapper mapper;
    private UserMapper userMapper;

    @Override
    public List<Permission> getPermissionsByPage(Page<Permission> page) {
        return mapper.selectList( page, null);
    }

    @Override
    public List<Permission> getAllPermissionsByPage(Page<Permission> page) {
        return null;
    }

    @Override
    public IPage<Permission> getPermissionsByPageWithCondition(Page<Permission> page, String keyword) {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)){
            wrapper.like("perm_name",keyword)
                    .or().like("perm_key",keyword);
        }
        return mapper.selectPage(page,wrapper);
    }
    @Override
    public List<Permission> getPermissionsByUserId(Long userId) {
//        //实际项目中,判断逻辑会包含判断用户是否被禁用、角色是否被禁用、权限是否被禁用
            // 基础参数校验
            if (userId == null || userId <= 0) {
                return Collections.emptyList();
            }

            // 特殊情况处理：用户ID为0无效
            if (userId == 0) {
                return Collections.emptyList();
            }

            // 超级管理员(用户ID为1)拥有所有权限
            if (userId == 1) {
                return mapper.selectList(null);
            }

            // 实际项目中应包含的判断逻辑：
            // 1. 判断用户是否被禁用
            User user = userMapper.selectById(userId);  // 修正：使用注入的mapper实例
            if (user == null || user.getStatus() != 1) { // 假设1为启用状态
                return Collections.emptyList();
            }

            // 2. 获取用户权限（通过角色关联）
            List<Permission> permissions = mapper.getPermissionsByUserId(userId);

            // 3. 判断权限是否被禁用
            return permissions.stream()
                    .filter(permission -> permission.getDeleted() == 0) // 未被删除
                    .collect(Collectors.toList());
    }

    @Override
    public List<Permission> getPermissionsByRoleId(Long roleId) {

        return mapper.getPermissionsByRoleId(roleId);

    }

    @Override
    public Set<String> getPermissionCodesByUserId(Long userId) {
        List<Permission> list = getPermissionsByUserId(userId);
        return list.stream() //将list转换为流
                .map(Permission::getPermKey) //获取权限key
                .collect(Collectors.toSet()); //转换成Set
    }

    @Override
    public Set<String> getPermissionCodesByRoleId(Long roleId) {
        List<Permission> permissions = getPermissionsByRoleId(roleId);
        return permissions.stream()
                .map(Permission::getPermKey)
                .collect(Collectors.toSet());
    }

    @Override
    public Set<String> getPermissionsByUserIdSet(Set<Long> userId) {
        return null;
    }

    @Override
    public Set<String> getPermissionsByRoleIdSet(Set<Long> roleId) {
        return null;
    }
}
